import 'package:dailoz/constants.dart';
import 'package:dailoz/data/page_data.dart';
import 'package:dailoz/size_config.dart';
import 'package:dailoz/views/add_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class BottomNavigatorBar extends StatefulWidget {
  const BottomNavigatorBar({
    Key? key,
  }) : super(key: key);

  @override
  State<BottomNavigatorBar> createState() => _BottomNavigatorBarState();
}

class _BottomNavigatorBarState extends State<BottomNavigatorBar> {
  Widget barItem(BuildContext context, IconData? icon, int index) {
    Color? iconColor = navigationBarIconColor;
    int currentIndex = Provider.of<PageData>(context).index;
    if (currentIndex == index) {
      // 选中
      iconColor = buttonWidgetColor;
    }
    return GestureDetector(
      child: Icon(
        icon,
        size: SizeConfig.getProportionateFontSize(28.0),
        color: iconColor,
      ),
      onTap: () {
        if (currentIndex != index) {
          setState(() {
            Provider.of<PageData>(context, listen: false).increment(index);
          });
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: SizeConfig.getProportionateScreenHeight(74.0),
      width: SizeConfig.getProportionateScreenWidth(336.0),
      margin: EdgeInsets.only(
          left: SizeConfig.getProportionateScreenWidth(28.0),
          bottom: SizeConfig.getProportionateScreenWidth(5.0)),
      decoration: BoxDecoration(
        color: Theme.of(context).bottomAppBarColor,
        borderRadius: const BorderRadius.all(
          Radius.circular(14.0),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          barItem(context, Icons.home_outlined, 0),
          barItem(context, Icons.task_outlined, 1),
          GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, AddPage.id);
            },
            child: CircleAvatar(
              radius: SizeConfig.getProportionateScreenHeight(25.0),
              child: Icon(
                Icons.add,
                size: SizeConfig.getProportionateFontSize(28.0),
              ),
            ),
          ),
          barItem(context, Icons.analytics_outlined, 3),
          barItem(context, Icons.person_outline_outlined, 4),
        ],
      ),
    );
  }
}
